<template>
<div>
  <el-row>
    <el-col :span="5"></el-col>
    <el-col :span="15" style="margin-top: 5vh">
      <span>
        <el-select v-model="page.hasReply" class="m-2" placeholder="Select">
          <el-option
            v-for="item in page.hasReplyOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </span>
      <el-table :data="messageList" style="width: 100%">
        <el-table-column type="index" :index="index" width="180"/>
        <el-table-column prop="content" label="content" width="360" />
        <el-table-column prop="createTime" label="createTime" width="180" />
        <el-table-column prop="state" label="state" />
        <el-table-column label="操作" min-width="180" header-align="center" align="center">
          <template #default="props">
            <el-button type="primary" @click="view(props.row.id, props.row.content)" >回复</el-button >
            <!-- <el-button type="danger" @click="del(props.row.id)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-left: 20vw; margin-top: 2vh;"
        :currentPage="page.currentPage"
        :page-size="page.pageSize"
        layout="prev, pager, next"
        :total="page.num"
        @current-change="handleCurrentChange"
      />
    </el-col>
  </el-row>
</div>
</template>

<script>
import api from '../../api/data.js'
import { ElMessage } from 'element-plus'
export default {
  name: 'AdminBoard',
  components: {

  },
  data () {
    return {
      messageList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        num: 1,
        hasReply: 0,
        hasReplyOpt: [
          {label: '全部', value: 0,},
          {label: '未回复', value: 1,},
        ],
      }
  }},
  methods: {
    view (id, content) {
      this.$router.push({path: '/admin/boardReply', query: {pid: id, content: content}})
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.pageChange()
    },
    pageChange () {
      api.aGetAllMessages({
        token: this.$store.state.token,
        pageSize: this.page.pageSize,
        currentPage: this.page.currentPage,
        hasReply: this.page.hasReply
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.messageList = res.data.messageList
          this.page.pageSize = res.data.pageSize
          this.page.currentPage = res.data.currentPage
          this.page.num = res.data.pageNum * res.data.pageSize
          this.messageList.forEach((item) => {
            if (item.child.length)
              item.state = '已回复'
            else 
              item.state = '未回复'
          })
          // console.log(this.page)
        } else {
          ElMessage.error(res.message)
        }
      })
    }
  },
  mounted () {
    this.pageChange()
  },
  computed: {
    sel1 () {
			return this.page.hasReply
		},
  },
  watch: {
    sel1 () {
      this.pageChange()
    },
  }
}
</script>

<style scoped>

</style>
